package com.liella.controller;

import com.liella.entity.Video;
import com.liella.model.vo.PageResult;
import com.liella.model.vo.Result;
import com.liella.service.TVideoService;
import com.liella.service.VedioService;
import com.liella.utils.FastDFSUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Api(tags = "视频模块")
@RestController
public class VedioController {
    @Autowired
    private VedioService vediorService;
    @Autowired
    private TVideoService tVideoService;

    @Autowired
    private FastDFSUtil fastDFSUtil;

    /**
     * 获取文件唯一标识MD5
     * @param file
     * @return
     * @throws Exception
     */
    @PostMapping("/md5files")
    public Result<String> getFileMD5(MultipartFile file) throws Exception {
        String fileMD5 = vediorService.getFileMD5(file);
        return  Result.success(fileMD5);
    }

    /**
     * 分片上传文件
     * @param slice 文件
     * @param fileMd5 唯一标识
     * @param sliceNo 初始分片 默认为1
     * @param totalSliceNo 总分片书
     * @return {@link Result<String>} 文件地址
     * @throws Exception 异常
     */
    @PutMapping("/file-slices")
    public Result<String> uploadFileBySlices(MultipartFile slice,
                                             String fileMd5,
                                             Integer sliceNo,
                                             Integer totalSliceNo) throws Exception {
        String filePath = vediorService.uploadFileBySlices(slice, fileMd5, sliceNo, totalSliceNo);
        return  Result.success(filePath);
    }

    /**
     * 上传视频
     * @param slice 视频
     * @return {@link Result<String>} 视频地址
     */
    @PutMapping("/up-vedio")
    @ApiOperation(value = "上传视频")
    public Result<String> uploadVedio(MultipartFile slice) throws Exception {
        String filePath = vediorService.uploadVedio(slice);
        return  Result.success(filePath);
    }
    /**
     * 获取文件唯一标识MD5
     * @param file
     * @return {@link Result<String>} 文件标识
     * @throws Exception
     */
    @PostMapping("/md5filess")
    public Result<String> getFileMD5s(MultipartFile file) throws Exception {
        Integer integer = fastDFSUtil.convertFileToSlices(file);
        return  Result.success(String.valueOf(integer));
    }

    //投稿视频
    /**
     * 投稿视频
     * @param video 视频及其详细信息
     * @return {@link Result<String>} 成功或者失败
     */
    @PostMapping("/upve")
    @ApiOperation(value = "投稿视频")
    public Result<String> upve(@RequestBody Video video) {
      tVideoService.saveVideo(video);
      return Result.success("投稿成功");

    }

    /**
     * 查看首页视频
     * @return {@link Result<PageResult<Video>>} 首页视频
     */
    @GetMapping("/video/list")
    public Result<PageResult<Video>> page() {
      return Result.success(tVideoService.pages());

    }


}
